import $util from "@/utils/index.js"
import $api from "@/api/index.js"
import $store from "@/store/index.js"
import {
	req
} from '@/utils/req.js';
export default {
	state: {
		autograph: '',
		userInfo: {},
		appLogin: '',
		loginType: '',
		loginPage: '',
		isGzhLogin: false,
		isShowLogin: false,
		locationChange: false,
		auth_location: false,
		location: {
			lat: '',
			lng: '',
			address: '',
			is_util_loca: 0
		},
		city_info: {},
		locaRefuse: false,
		changeAddr: false,
		isUtilLoca: false,
		isChangeCoachLoca: false,
		appCheckLocaStatus: null,
		isShowAuth: true,
		mineInfo: {}, // 用户个人中心
		coachInfo: {},
		userCoachStatus: {},
		changeOnAddr: {},
		noChangeLoca: {
			noloca: false
		},
		haveShieldOper: 0,
		personVerifyUrl: '',
		fddExtsign: '',
		oldMovable: {
			x: 0,
			y: 0
		},
		userPrivacySetting: {},
		resolvePrivacy: {},
		wecom_staff: '',
		scanRecordId: 0,
		is_register: 0,
		appShare: false,
		isConsole: false,
		userAccountInfo: {},
		coachAccountInfo: {},
	},
	mutations: {
		//更新内容
		async updateUserItem(state, item) {
			let {
				key,
				val
			} = item
			if (key == 'userInfo' && val && val.id) {
				let {
					phone = '',
						create_time,
						alipay_number = '',
						bank_card_id = ''
				} = val
				if (phone) {
					val.split_phone = phone.substring(0, 3) + '****' + phone.substring(7, 11)
				}
				val.create_date = $util.formatTime(create_time * 1000, 'YY-M-D')
				if (alipay_number) {
					let len = alipay_number.length
					val.split_alipay_number = alipay_number.substring(0, len > 3 ? 3 : 1) + '***'
					if (len > 6) {
						val.split_alipay_number += alipay_number.substring(len == 6 ? len - 2 :
							len - 3, len)
					}
				}
				if (bank_card_id) {
					val.split_card_id = bank_card_id.substring(0, 4) + '***' + bank_card_id.substring(
						bank_card_id.length - 4, bank_card_id.length)
				}
				state.isConsole = val.nickName == '测试用户 console'
			}

			if (key == 'mineInfo' && val && val.id == -1) {
				let storeArr = ['userInfo', 'appLogin', 'loginType', 'loginPage', 'isShowLogin',
					'isGzhLogin'
				]
				storeArr.map(key => {
					let syncVal = key == 'isShowLogin' ? true : ''
					state[key] = syncVal
					uni.setStorageSync(key, syncVal)
				})
			}

			if (['autograph', 'userInfo', 'city_info', 'auth_location', 'location', 'changeAddr', 'appLogin',
					'loginType',
					'loginPage',
					'isShowLogin', 'isGzhLogin', 'scanRecordId', 'is_register'
				].includes(key)) {
				uni.setStorageSync(key, val)
			}

			// #ifdef APP-PLUS
			if (key === 'location') {
				let locaRefuse = (!val || (val && val.hasOwnProperty('lat') && !val.lat && !val.lng)) ? true : false
				state.locaRefuse = locaRefuse
				uni.setStorageSync('locaRefuse', locaRefuse)
			}

			if (key === 'locaRefuse') {
				uni.setStorageSync('locaRefuse', val)
			}
			// #endif

			state[key] = val
		}
	},
	actions: {
		//获取个人信息
		async getUserInfo({
			commit,
			state
		}, param) {
			let data = await $api.user.userInfo()
			let {
				id
			} = data
			if (!id) {
				data = {
					avatarUrl: `https://www.peikangbao.cn/admin/anmo/mine/default-user.png`
				}
			}
			commit('updateUserItem', {
				key: 'userInfo',
				val: data
			})
		},
		//获取用户个人中心数据
		async getMineInfo({
			commit,
			state
		}, param) {
			let data = await $api.mine.index()
			let {
				id = 0,
					fx_status
			} = data
			if (!id) {
				data = {
					id: -1
				}
			}
			let {
				fx_check = 0
			} = $store.state.config.configInfo
			data.is_fx = !fx_check || (fx_check && fx_status == 2)
			commit('updateUserItem', {
				key: 'mineInfo',
				val: data
			})
		},
		//获取技-师信息
		async getCoachInfo({
			commit,
			state
		}, param) {
			try {
				let data = await $api.technician.coachIndex()
				commit('updateUserItem', {
					key: 'coachInfo',
					val: data
				})
				let {
					id = 0,
						status = 0,
						coach_position = 0,
						lat = 0,
						lng = 0,
						address = ''
				} = data
				let val = Object.assign({}, state.userCoachStatus, {
					status,
					coach_id: id,
					coach_position,
					auto_coach_position: coach_position,
					lat,
					lng,
					address
				})
				let mval = Object.assign({}, state.mineInfo, {
					coach_status: status,
				})
				commit('updateUserItem', {
					key: 'userCoachStatus',
					val
				})
				commit('updateUserItem', {
					key: 'mineInfo',
					val: mval
				})
			} catch (e) {
				let data = await $api.mine.index()
				let {
					id = 0,
						fx_status = 0,
						coach_id = 0,
						coach_status = -1,
						sh_text = '',
				} = data
				if (!id) {
					data = {
						id: -1
					}
				}
				let {
					fx_check = 0
				} = $store.state.config.configInfo
				data.is_fx = !fx_check || (fx_check && fx_status == 2)
				commit('updateUserItem', {
					key: 'mineInfo',
					val: data
				})
				commit('updateUserItem', {
					key: 'coachInfo',
					val: {
						id: coach_id,
						status: coach_status,
						sh_text,
						user_id: data.id
					}
				})
				let val = Object.assign({}, state.userCoachStatus, {
					status: coach_status
				})
				commit('updateUserItem', {
					key: 'userCoachStatus',
					val
				})
			}
		},
		//获取技-师信息
		async getAccountCoach({
			commit,
			state
		}) {
			let data = await $api.mine.getAccountCoach()
			if (data == null || data == '') {
				data = {
					id: 0
				}
			}
			commit('updateUserItem', {
				key: 'coachAccountInfo',
				val: data
			})
		},
		//获取技-师账户信息
		async getCoachAccount({
			commit,
			state
		}, param = {
			delay: 0
		}) {
			let data = await $api.mine.getCoachWalletAccount()
			let {
				alipay_number = '',
					bank_card_id = ''
			} = data
			if (alipay_number) {
				let len = alipay_number.length
				data.split_alipay_number = alipay_number.substring(0, len > 3 ? 3 : 1) + '***'
				if (len > 6) {
					data.split_alipay_number += alipay_number.substring(len == 6 ? len - 2 :
						len - 3, len)
				}
			}
			if (bank_card_id) {
				data.split_card_id = bank_card_id.substring(0, 4) + '***' + bank_card_id.substring(
					bank_card_id.length - 4, bank_card_id.length)
			}
			commit('updateUserItem', {
				key: 'userAccountInfo',
				val: data
			})
		},
		//获取技-师信息
		async getUserCoachStatus({
			commit,
			state
		}, param) {
			let data = await $api.mine.getUserCoachStatus()
			let {
				status = -1,
					coach_position = 0
			} = data
			data.auto_coach_position = coach_position
			commit('updateUserItem', {
				key: 'userCoachStatus',
				val: data
			})
		},
		//获取客服
		async getWecomStaff({
			commit,
			state
		}, param) {
			let {
				im_type = 1
			} = $store.state.config.configInfo
			if (im_type == 3) {
				let data = await $api.mine.getWecomStaff()
				commit('updateUserItem', {
					key: 'wecom_staff',
					val: data
				})
			}
		},
		//添加扫码记录
		async addScanRecord({
			commit,
			state
		}, param) {
			let flag = true
			// #ifdef H5
			if (window.location.href.indexOf('?#') < 0) {
				flag = false
			}
			// #endif
			if (flag) {
				if (!param.hasOwnProperty('is_qr')) {
					param.is_qr = 1
				}
				let data = await $api.mine.addScanRecord(param)
				if (data * 1 > 0) {
					commit('updateUserItem', {
						key: 'scanRecordId',
						val: data
					})
				}
			}
		},
		//更新扫码记录
		async updateScanRecord({
			commit,
			state
		}, param = {}) {
			console.log(param, "====param updateScanRecord")
			let flag = true
			let {
				is_login = 0
			} = param
			// #ifdef H5
			if (!is_login && window.location.href.indexOf('?#') < 0) {
				flag = false
			}
			// #endif
			let {
				scanRecordId: id,
				is_register = 0,
				userInfo = {
					id: 0
				}
			} = state
			console.log(id, is_register, "====updateScanRecord id is_register")
			if (flag && id && userInfo && userInfo.id) {
				$api.mine.updateScanRecord({
					id,
					is_register
				}).then(res => {
					commit('updateUserItem', {
						key: 'scanRecordId',
						val: 0
					})
				})
			}
		},
		// 获取用户信息
		async getAuthUserProfile({
			commit,
			state
		}, param) {
			let {
				nickName,
				avatarUrl
			} = param
			await $api.user.userUpdate(param)
			let data = Object.assign({}, state.userInfo, {
				nickName,
				avatarUrl
			})
			commit('updateUserItem', {
				key: 'userInfo',
				val: data
			})
		},
		// 获取手机号
		async getAuthPhone({
			commit,
			state
		}, {
			e = {
				detail: {}
			},
			must = false
		} = {}) {
			let {
				encryptedData = '', iv = ''
			} = e.detail;
			let phone = ''
			if (encryptedData && iv) {
				let phone = await $api.user.reportPhone({
					decode: 1,
					encryptedData: encodeURIComponent(encryptedData),
					iv
				})
				let data = Object.assign({}, state.userInfo, {
					phone
				})
				commit('updateUserItem', {
					key: 'userInfo',
					val: data
				})
				return phone;
			}
		},
		async getPrivacySetting({
			commit,
			state
		}, param) {
			// #ifdef MP-WEIXIN
			wx.getPrivacySetting({
				success: (res) => {
					// 返回结果为: res = { needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》' }
					commit('updateUserItem', {
						key: 'userPrivacySetting',
						val: res
					})
				}
			})
			// #endif
			// #ifndef MP-WEIXIN
			commit('updateUserItem', {
				key: 'userPrivacySetting',
				val: {
					needAuthorization: false,
					privacyContractName: ''
				}
			})
			// #endif
		},
		// 更新公共参数
		async updateCommonOptions({
			commit,
			state
		}, param) {
			let target = {}
			if (param.scene) {
				let res = await $api.base.getWxCodeData({
					code_id: param.scene
				})
				if (res && res.data) {
					target = $util.pick(res.data, ['pid',
						'coupon_atv_id', 'admin_id',
						'channel_id',
						'channel_qr_id',
						'salesman_id', 'partner_id',
						'level_reseller_id', 'coach_id',
					])
				}
			}
			let data = Object.assign({}, param, target)
			if (data.scene) {
				delete data.scene
			}
			// console.log(target, data, "=====updateCommonOptions target, data")
			return data
		}
	}
}